package spring_advice;

import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.support.DelegatingIntroductionInterceptor;

public class ControllablePerformanceMonitor extends DelegatingIntroductionInterceptor implements Monitorable {


    private ThreadLocal<Boolean> monitorSattusMap = new ThreadLocal<Boolean>();

    public void setMonitorable(boolean active) {

        monitorSattusMap.set(active);

    }


    public Object invoke(MethodInvocation mi) throws Throwable {
        Object obj = null;
        if (monitorSattusMap.get() != null && monitorSattusMap.get()) {
            PerformanceMonitor.begin(mi.getClass().getName() + "." + mi.getMethod().getName());
            obj = super.invoke(mi);
            PerformanceMonitor.end();
        } else {
            obj = super.invoke(mi);
        }
        return obj;
    }
}
